package cc.mrbird.assets.service.impl;

import cc.mrbird.assets.dao.AssetTypeMapper;
import cc.mrbird.assets.domain.AssetType;
import cc.mrbird.assets.service.AssetTypeService;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.service.impl.BaseService;
import cc.mrbird.system.domain.User;
import cc.mrbird.unit.dao.UnitTypeMapper;
import cc.mrbird.unit.domain.UnitType;
import cc.mrbird.unit.service.UnitTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("assetTypeService")
public class AssetTypeServiceImpl extends BaseService<AssetType> implements AssetTypeService {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AssetTypeMapper assetTypeMapper;
    @Override
    public List<AssetType> findAllList(AssetType assetType, QueryRequest request) {
        try {
            Example example = new Example(AssetType.class);
            Example.Criteria criteria = example.createCriteria();
            example.setOrderByClause("asset_type_id");
            return this.selectByExample(example);
        } catch (Exception e) {
            log.error("error", e);
            return new ArrayList<>();
        }
    }

    @Override
    public AssetType findByName(String asset_type_name) {
        Example example = new Example(AssetType.class);
        example.createCriteria().andCondition("lower(asset_type_name)=", asset_type_name.toLowerCase());
        List<AssetType> list = this.selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public void add(AssetType assetType) {
        this.save(assetType);
    }

    @Override
    public void update(AssetType assetType) {
        this.updateNotNull(assetType);
    }

    @Override
    public AssetType findById(Long id) {
        return this.selectByKey(id);
    }

    @Override
    @Transactional
    public void delete(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        this.batchDelete(list, "asset_type_id", AssetType.class);
    }
}
